﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.HeadUpWindow.CApSmartNoteDataContainer
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.SmartNotes;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;
using UiInternalControls;


namespace DnProcessSimulateCommands.HeadUpWindow;

public class CApSmartNoteDataContainer : TxDataContainer
{
  private const string NoteStatusPropertyName = "Status";
  private const string NoteHistoryText = "HistoryText";
  private const string NoteIconFileNamePropertyName = "NoteIconFile";
  private const string NoteDescription = "NoteText";
  private string _NoteName;
  private string _NoteType;
  private string _HistoryText;
  private string _DescText;
  private int _status;
  private bool _applyEnabled;
  private TxNote _note;

  internal Dictionary<string, object> PendingProperties { set; get; } = new Dictionary<string, object>();

  internal HashSet<string> PendingRemoveProperties { set; get; } = new HashSet<string>();

  public TxPictureDataContainer IconContainer { get; private set; }

  public CApSmartNoteDataContainer() => this.IconContainer = new TxPictureDataContainer();

  public bool ApplyEnabled
  {
    get => this._applyEnabled;
    set
    {
      this._applyEnabled = value;
      this.OnPropertyChanges(nameof (ApplyEnabled));
    }
  }

  public string NoteType
  {
    get => this._NoteType;
    set
    {
      this._NoteType = value;
      this.OnPropertyChanges(nameof (NoteType));
    }
  }

  public string NoteName
  {
    get => this._NoteName;
    set
    {
      this._NoteName = value;
      this.OnPropertyChanges(nameof (NoteName));
    }
  }

  public string HistoryText
  {
    get => this.PropertyValue(nameof (HistoryText), this._HistoryText);
    set
    {
      this.UpdatePendingProperty(nameof (HistoryText), this._HistoryText, value);
      this.OnPropertyChanges(nameof (HistoryText));
    }
  }

  public string DescText
  {
    get => this.PropertyValue("NoteText", this._DescText);
    set
    {
      this.UpdatePendingProperty("NoteText", this._DescText, value);
      this.OnPropertyChanges(nameof (DescText));
    }
  }

  public int Status
  {
    get => Convert.ToInt32(this.PropertyValue(nameof (Status), this._status.ToString()));
    set
    {
      this.UpdatePendingProperty(nameof (Status), this._status.ToString(), value.ToString());
      this.OnPropertyChanges(nameof (Status));
    }
  }

  internal void UpdateApply()
  {
    this.ApplyEnabled = this.PendingProperties.Count > 0 || this.PendingRemoveProperties.Count > 0;
  }

  internal void UpdatePendingProperty(
    string propertyName,
    string propertyOriginalValue,
    string propertyNewVal)
  {
    string str = propertyOriginalValue;
    object obj = (object) null;
    if (this.PendingProperties.TryGetValue(propertyName, out obj))
      str = obj as string;
    if (!(str != propertyNewVal))
      return;
    if (propertyOriginalValue != propertyNewVal)
      this.PendingProperties[propertyName] = (object) propertyNewVal;
    else
      this.PendingProperties.Remove(propertyName);
    this.UpdateApply();
  }

  internal string PropertyValue(string propertyName, string propertyOriginalValue)
  {
    string empty = string.Empty;
    object obj = (object) null;
    return !this.PendingProperties.TryGetValue(propertyName, out obj) ? propertyOriginalValue : obj as string;
  }

  public void Apply()
  {
    List<Tuple<string, object>> properties = TxGenericNoteEx.GetProperties(this._note);
    List<Tuple<string, object>> tupleList = new List<Tuple<string, object>>();
    foreach (Tuple<string, object> tuple in properties)
    {
      object obj;
      if (this.PendingProperties.TryGetValue(tuple.Item1, out obj))
      {
        tupleList.Add(new Tuple<string, object>(tuple.Item1, obj));
        this.PendingProperties.Remove(tuple.Item1);
      }
      else if (!this.PendingRemoveProperties.Contains(tuple.Item1))
        tupleList.Add(tuple);
    }
    foreach (KeyValuePair<string, object> pendingProperty in this.PendingProperties)
      tupleList.Add(new Tuple<string, object>(pendingProperty.Key, pendingProperty.Value));
    TxGenericNoteEx.ResetAllProperties(this._note, tupleList);
    this.PendingProperties.Clear();
    this.PendingRemoveProperties.Clear();
    this.SetModifiableDataFromNote();
    this.UpdateApply();
  }

  public virtual void Load(TxNote note)
  {
    this._note = note;
    this.NoteType = this._note.LocalizedType();
    this.NoteName = this._note.Name;
    this.SetModifiableDataFromNote();
    this.IconContainer.Load(TxGenericNoteEx.ComputeIconBySize(note, 24));
  }

  private void SetModifiableDataFromNote()
  {
    this._DescText = this._note.Text;
    this._HistoryText = TxGenericNoteEx.NotePropertyOrDefault(this._note, "HistoryText", "") as string;
    this._status = Convert.ToInt32(TxGenericNoteEx.NotePropertyOrDefault(this._note, "Status", "0") as string);
    this.DescText = this._DescText;
    this.HistoryText = this._HistoryText;
    this.Status = this._status;
  }
}
